package br.com.salaodebeleza.view;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import br.com.salaodebeleza.business.TbFuncionarioBC;
import br.com.salaodebeleza.domain.TbFuncionario;
import br.gov.frameworkdemoiselle.annotation.PreviousView;
import br.gov.frameworkdemoiselle.stereotype.ViewController;
import br.gov.frameworkdemoiselle.template.AbstractEditPageBean;
import br.gov.frameworkdemoiselle.transaction.Transactional;

@ViewController
@PreviousView("./tbFuncionario_list.jsf")
public class TbFuncionarioEditMB extends AbstractEditPageBean<TbFuncionario, Long> {

	private static final long serialVersionUID = 1L;

	@Inject
	private TbFuncionarioBC tbFuncionarioBC;
	
	@Override
	@Transactional
	public String delete() {
		this.tbFuncionarioBC.delete(getId());
		return getPreviousView();
	}
	
	@Override
	@Transactional
	public String insert() {
		this.tbFuncionarioBC.insert(getBean());
		return getPreviousView();
	}
	
	@Override
	@Transactional
	public String update() {
		this.tbFuncionarioBC.update(getBean());
		return getPreviousView();
	}
	
	@Override
	protected void handleLoad() {
		setBean(this.tbFuncionarioBC.load(getId()));
	}
	
	public void validarEspacoBranco(FacesContext context, UIComponent componet, Object object){
		String valor = (String) object;
		if(valor.trim().equals("")){
			((UIInput) componet).setValid(false);
			String mensagem = "Valor invalido, preencha com caracteres diferentes de espaço.";
			FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, mensagem, mensagem);
			context.addMessage(componet.getClientId(context), facesMessage);
		}
	}

}